package com.guapi.service;

import com.guapi.dao.GoodsMapper;
import com.guapi.entity.Goods;
import com.guapi.entity.User;
import com.guapi.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public List<Goods> findGoodsList(int modern, int status, int offset, int limit){
        return goodsMapper.selectAllGoods(modern, status, offset, limit);
    }

    public int findGoodsCount(int status){
        return goodsMapper.selectCountGoods(status);
    }

    public Goods findGoodsById(int goodsId){
        return goodsMapper.selectGoodsByGoodsId(goodsId);
    }
    public Set<Goods> getCartGoods(User user){

        //if (user == null) {
        //    throw new IllegalArgumentException("您好，请先登录！");
        //}
        String redisKey = RedisKeyUtil.getUserShoppingCartKey(user.getId());

        //goodsSet.addAll(redisTemplate.opsForZSet().score(redisKey));
        return redisTemplate.opsForZSet().range(redisKey, 0, redisTemplate.opsForZSet().size(redisKey));
    }

    public Long removeCartGoods(User user, int goodsId) {
        String redisKey = RedisKeyUtil.getUserShoppingCartKey(user.getId());
        Goods goods = findGoodsById(goodsId);
        return redisTemplate.opsForZSet().remove(redisKey, goods);
    }

    public int addGoods(Goods goods){
        return goodsMapper.insertGoods(goods);
    }


}
